/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.lottery.game.core.modules.service.app;

import com.lottery.game.common.constants.GameConstants;
import com.lottery.game.core.modules.entity.user.ChatRecord;
import com.lottery.game.core.modules.entity.user.UserRankPojo;
import com.lottery.game.core.modules.mapper.ChatRecordDao;
import com.lottery.game.core.modules.service.game.SysDictService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 聊天记录管理Service
 * 
 * @author ss
 * @version 2018-05-11
 */
@Service
public class ChatRecordService {

	@Autowired
	private ChatRecordDao chatRecordDao;

	@Autowired
	private SysDictService sysDictService;
	
	public List<ChatRecord> queryChatRecordByRoomnumber(String roomNumber, int pageNumber, int pageSize)
			throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		// 计算分页的起始页
		map.put("pageNumber", (pageNumber - 1) * pageSize);
		// 这里传入数据总条数
		map.put("pageSize", pageSize);
		map.put("roomNumber", roomNumber);
		List<ChatRecord> crList = this.chatRecordDao.queryChatRecordByRoomnumber(map);
		String imageServerAddress = sysDictService.findImageServerAddress();
		for (ChatRecord chatRecord : crList) {
			chatRecord.setLevel_photourl(chatRecord.getLevel_photourl() == null ? null : imageServerAddress + chatRecord.getLevel_photourl());
			chatRecord.setSenderphoto(chatRecord.getSenderphoto() == null ? null : imageServerAddress + chatRecord.getSenderphoto());
		}
		return crList;
	}

	public List<ChatRecord> queryChatRecordByRoomnumberByUser(String roomNumber, int pageNumber, int pageSize,
			String userId) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		// 计算分页的起始页
		map.put("pageNumber", (pageNumber - 1) * pageSize);
		// 这里传入数据总条数
		map.put("pageSize", pageSize);
		map.put("roomNumber", roomNumber);
		map.put("userId", userId);
		/*
		 * List<ChatRecord> crList =
		 * this.chatRecordDao.queryChatRecordByRoomnumberByUser(map); for (ChatRecord
		 * chatRecord : crList) { chatRecord.getLevel_photourl(); }
		 */
		return this.chatRecordDao.queryChatRecordByRoomnumberByUser(map);
	}

	public void addChatRecord(ChatRecord chatRecord) {
		this.chatRecordDao.insertChatRecord(chatRecord);
	}

	/**
	 * 根据userid获取发送给他和他发送的数据 type 为 customerservicechat
	 * 
	 * @param userId
	 * @param pageNumber
	 * @param pageSize
	 * @return
	 * @throws Exception
	 */
	public List<ChatRecord> queryCustomerServiceChatRecordByUser(String userId, int pageNumber, int pageSize)
			throws Exception {
		int startNum = (pageNumber - 1) * pageSize;
		Map<String, Object> map = new HashMap<>();
		map.put("startNum", startNum);
		map.put("pageSize", pageSize);
		map.put("userId", userId);
		// 用户发送
		map.put("chatType", GameConstants.CHATTYPE_CUSSERVICECHAT);
		// 客服回复
		map.put("chatType2", GameConstants.CHATTYPE_CUSREPLY);
		return this.chatRecordDao.queryCustomerServiceChatRecordByUser(map);
	}

	public String findCustomServieWelcomeMsg() {
		return this.chatRecordDao.fintCustomServieWelcomeMsg();
	}
}
